"use strict";
var utils = require('../../utils/utils.js');
var request = require('../../utils/request.js');
var auth = require('../../utils/auth.js');
var config = require('../../config.js');
var app = getApp();
const ZERO = 0;
var pageSetting = {
  // 页面显示后，进行登录和链接，完成后开始启动游戏服务

  data: {
    curData: {
      avatar: '',
      user_name: '',
      company: '',
      id: ''
    },
    account: [],
    curIndex: 0,
    money: '',
    name: '',
    hidePage: true
  },
  //选择区域
  //  selectOk: function(event){
  //    var selectId = event.target.dataset.id;
  //    for (var i = 0; i < this.data.items.length; i++) {
  //      if (this.data.items[i].id == selectId) {
  //        this.data.items[i].checked = true;
  //        this.data.name = this.data.items[i].name;
  //        console.log(this.data.name);
  //      } else {
  //        this.data.items[i].checked = false;
  //      }
  //    }
  //    this.setData({
  //      items:this.data.items,
  //    })
  // },
  onLoad() {
    //有服务的时候
    var that = this;
    that.init();
    
  },
  onShow() {
    var that = this;


    var next = function(){
      var avatar = wx.getStorageSync('user_info')['head_img'];
      that.setData({
        'curData.avatar': avatar,
        hidePage: false
      })
      that.getAccountInfo();
      // console.log(avatar);
      // debugger;
      // wx.setStorageSync('id',avatar);
    }
    auth(next);
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;

      var id = wx.getStorageSync('id');
      var ratingData = wx.getStorageSync('ratingData');

      let j = 0;
      for(let i = 0; i<ratingData.length; i++){
          if(ratingData[i].id == id){
              j = i;
          }
      }
      var curData = ratingData[j];

      that.setData({
          'account': ratingData,
          'curData.user_name': curData['user_name'],
          'curData.company':  curData['company'],
          'curData.id':  curData['id']
      });

  },
  onHide() {

  },
  init() {
    new app.WeToast();
  },
  bindInputTap(e) {
    var value = e.detail.value;
    var cinput = e.currentTarget.dataset.cinput;
    if(cinput == 'money'){

      this.setData({
        'money': this.clearNoNum(value)
      })
    }else if (cinput == 'name'){
      this.setData({
        'name': value
      })
    }
  },
  clearNoNum(value){  
    value = value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
    value = value.replace(/^\./g,"0."); //如果第一位就是. 变成0.
    value = value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的   
    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");  
    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');//只能输入两个小数   
    if(value.indexOf(".")< 0 && value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额  
     value= parseFloat(value.substring(0, 5));//最长整数位5位数
    }
    return value;
  },
  formSubmit(e) {
    var that = this;
    var data = e.detail.value;

    if (!that.data.money) {
      wx.showToast({
        icon: 'none',
        title: '付款金额必填'
      })
      return false;
    }

    if(parseFloat(that.data.money).toFixed(2) <= ZERO.toFixed(2)){
      wx.showToast({
        icon: 'none',
        title: '付款金额错误'
      })
      return false;
    }

    wx.navigateTo({
      url: `/pages/collection/collection?money=${data.money}&name=${data.name}&id=${data.id}`
    });

  },
  scanTap(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    if(!that.data.money){
      wx.showToast({
        icon: 'none',
        title: '付款金额必填'
      })
      return false;
    }

    if(parseFloat(that.data.money).toFixed(2) <= ZERO.toFixed(2)){
      wx.showToast({
        icon: 'none',
        title: '付款金额错误'
      })
      return false;
    }
    wx.scanCode({
      success: function(res) {

        var data = {
          id: id,
          code: res.result,
          money: that.data.money,
          name: that.data.name
        }

        //支付
        request.get({'path': config.createOrder}, data).then(function(res){
          var ratingData = res.data;
          if (ratingData.ret == 0) {
              that.setData({
                'money': '',
                'name': ''
              })
            wx.navigateTo({
              url: `/pages/list/list/list?id=${id}money=${that.data.money}`
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: ratingData.msg
            })
            return false;
          }
        }, function(res){
          wx.showToast({
            icon: 'none',
            title: res.data.msg
          })
          return false;
        })
      },
      fail: function(res) {
        wx.showToast({
          icon: 'none',
          title: '支付失败'
        })
      }
    })
  },
  goRecordTap(e){
    var id = e.currentTarget.dataset.id;
    if(id){
      wx.navigateTo({
        url: `/pages/list/list/list?id=${id}`
      })
    }
  },
  goOcr(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/ocr/ocr?id=${id}`
    })
  },
  startProgressAnimation(isProgressShow) {
    var that = this
    if (isProgressShow) {
      that.animation.bottom(0).step()
    } else {
      that.animation.bottom(-100 + '%').step()
    }
    that.setData({
      animationProgress: that.animation.export()
    })
  },
  showProgressTap(e){
    this.startProgressAnimation(true);
  },
  hideProgressTap: function (e) {
    this.startProgressAnimation(false);
  },
  switchAccountTap(e){
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var username = e.currentTarget.dataset.username;
    var company = e.currentTarget.dataset.company;

    this.setData({
      'curIndex': index,
      'curData.user_name': username,
      'curData.id': id,
      'curData.company': company
    });
    wx.setStorageSync('id',id)

    this.hideProgressTap();
  },
  changeAccountTap(){
    this.showProgressTap(true);
  },
  getAccountInfo(){
    var that = this;
      var mobile = wx.getStorageSync('mobile');
      var id = wx.getStorageSync('id');
    request.get({'path': config.getList}, {mobile: mobile}).then(function(res){
      var ratingData = res.data;
      if (ratingData.ret == 0 && ratingData.data.length > 0) {
        let data = ratingData.data[0];

        that.hideProgressTap();
      } else {
        wx.showToast({
          icon: 'none',
          title: ratingData.msg
        })
        return false;
      }
    }, function(res){
      wx.showToast({
        icon: 'none',
        title: res.data.msg
      })
      return false;
    })
  }
}
utils.assign(pageSetting);

Page(pageSetting);
